<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\store\model\Devices;
use app\common\model\Voucher;
use app\common\model\Voucherlog;
use app\common\model\Order;
use app\common\model\OrderGoods;
use app\common\model\Rebate;

/**
 * 闸机接口
 */
class Gate extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['consume'];

    
    
    /**
     * [consume 核销票接口]
     * @Author   ZhouYunpeng
     * @DateTime 2020-06-23
     * @param string $deviceid 设备ID
     * @param int $codetype 码内容类型
     * @param string $ticketcode 票号、订单号、身份证号
     * @param int $number 核销数量 
     * @return   [type]      [description]
     */
    public function consume()
    {   
        $apiname = $this->request->request('apiname');
        $deviceid = $this->request->request('deviceid');
        $codetype = $this->request->request('codetype');
        $ticketcode = $this->request->request('ticketcode');
        $number = $this->request->request('number');
        if ($apiname != 'consume') {
            $this->error(__('Invalid parameters'));
        }
        if (!$deviceid || !$codetype || !$ticketcode || !$number) {
            return  json(['code'=>0 ,'msg'=>__('Invalid parameters')]);
        }
        $devices = Devices::get(['devices_id' => $deviceid]);
        if ($devices) {
             // 设备是否锁定
            if ($devices['status'] == 'hidden') {  
                return json(['code'=>0 ,'msg'=>__('Devices is locked')]);
            }else{
                //核销
                if ($codetype == 1) {
                    $voucher = Voucher::get(['voucher_number' => $ticketcode,'use_status'=>1,'scenic_id'=>$devices['s_id']]);
                }elseif ($codetype == 2) {
                  # code...
                }
                
                if ($voucher) {
                    // 是否已经消费
                   if ($voucher['status'] == 1 && $voucher['buynum'] == $voucher['consum']) {
                      return json(['code'=>0 ,'msg'=>__('Already consumed')]);
                   }
                    $update = [
                        'consum' => $voucher['consum'] + $number ,
                        'no_check_num' => $voucher['no_check_num'] - $number ,
                        'check_time' => time(),
                    ];
                    if ($number  == $voucher['no_check_num']) {
                        //要验证数量与未验证数量相等为全部验证完
                        $update['status'] = 1;
                        $update['use_status'] = 1;
                    }
                    $order = Order::get($voucher['order_id']);
                    $log_data = [
                          'voucher_id' => $voucher['id'],
                          'voucher_num' => $number,
                          'order_id'    => $voucher['order_id'],
                          'scenic_id'   => $voucher['scenic_id'],
                          'goods_id' => $order['goods_id'],
                          'store_id' => $order['store_id'],
                          'user_id' => $order['user_id'],
                          'devices_id' => $deviceid,
                          'is_thirdparty' =>0,
                          'add_time' => date('Y-m-d H:i:s')
                      ];

                      Voucher::where(['id'=>$voucher['id']])->update($update);
                      Voucherlog::create($log_data);//凭证号日志
                      $orderGoods = new OrderGoods();
                      $orderGoods->checkTicket($voucher['voucher_number'],$number,$voucher['order_id']);
                      //进行返利
                      Rebate::executeRebate($voucher['order_id'],$number);
                      return json(['code'=>1,'msg'=>'接口请求成功','showtext'=>'票1','soundid'=>1,'state'=>1]);
                      
                }else{
                    return json(['code'=>0 ,'msg'=>__('Voucher not find')]);
                }

            }
           
        }else{
            return json(['code'=>0 ,'msg'=>__('Devices not find')]);
        } 
    }





}
